import axios from 'axios';

const http = axios.create({
  // baseURL: 'http://pcapi-xiaotuxian-front-devtest.itheima.net/',
  baseURL: 'https://apipc-xiaotuxian-front.itheima.net',
  timeout: 5000
})

http.interceptors.request.use(config => {
  return config
}, e => {
  return Promise.reject(e)
})

http.interceptors.response.use(response => {
  // const { success, message } = response.data
  // if (success) {
  //   return response
  // } else {
  //   return Promise.reject(message)
  // }
  return response.data
}, err => {
  return Promise.reject(err)
})

function createApi(url, method, reqDate?) {
  return http({
    url,
    method,
    [method.toUpperCase() === 'GET' ? 'params' : 'data']: reqDate
  })
}

export default createApi